import Vue from 'vue'
import Router from 'vue-router'
import constRouter from './constRouter'
import overseasRouter from './overseasRouter'

import db from 'utils/localstorage'

Vue.use(Router)

let virtual = db.get('virtual')
let router = new Router({
  // mode: 'history',
  routes: virtual == true ? overseasRouter : constRouter
  // routes: constRouter

})
console.log(router)
const whiteList = ['/login']

// let asyncRouter

// // 导航守卫，渲染动态路由
router.beforeEach((to, from, next) => {
  if(to && to.fullPath.indexOf("/login?token") != -1){
    next()
  }else{
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    }
    let token = db.get('USER_TOKEN')
    if (token.length) {
      next()
    } else {
      next('/login')
    }
  }
  
})

// function go (to, next) {
//   asyncRouter = filterAsyncRouter(asyncRouter)
//   router.addRoutes(asyncRouter)
//   next({...to, replace: true})
// }

// function save (name, data) {
//   localStorage.setItem(name, JSON.stringify(data))
// }

// function get (name) {
//   return JSON.parse(localStorage.getItem(name))
// }

// function filterAsyncRouter (routes) {
//   return routes.filter((route) => {
//     let component = route.component
//     if (component) {
//       switch (route.component) {
//         case 'MenuView':
//           route.component = MenuView
//           break
//         case 'PageView':
//           route.component = PageView
//           break
//         case 'EmptyPageView':
//           route.component = EmptyPageView
//           break
//         case 'HomePageView':
//           route.component = HomePageView
//           break
//         default:
//           route.component = view(component)
//       }
//       if (route.children && route.children.length) {
//         route.children = filterAsyncRouter(route.children)
//       }
//       return true
//     }
//   })
// }

// function view (path) {
//   return function (resolve) {
//     import(`@/views/${path}.vue`).then(mod => {
//       resolve(mod)
//     })
//   }
// }

export default router
